Qt之属性树

您所在的位置:网站首页 qt 属性页 Qt之属性树

Qt之属性树

2023-11-08 13:16| 来源: 网络整理| 查看: 265

在QT设计师中有一个很好用的部件属性设置窗口,qt却没有提供此控件也没有对应的例子,后来发现若安装qt时选择了安装源码,可以在源码中找到,在源码中搜索qtpropertybrowser即可。 在这里插入图片描述 由于之前不知道qt源码中有qtpropertybrowser的源码,因此自己实现了一个,效果如下 在这里插入图片描述 思路: 控件使用QTreewidget,设置属性的控件使用委托代理完成,根据不同的类型创建不同的代理控件,checkBox这类之间勾选的控件使用setItemWidget()方法设置到对应的列中, 当属性被改变时通过信号将属性名称和属性值发送出去.

代理类源码:继承至QItemDelegate

QWidget *TreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); QWidget *widget = creatrWidget(parent,index); return widget; } void TreeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { int type = index.data(PropertyTree::ROLETYPE).toInt(); switch (type) { case PropertyTree::COMBOX: { QComboBox *box = qobject_cast(editor); box->setCurrentText(index.data(Qt::EditRole).toString()); break; } case PropertyTree::TEXT: { QLineEdit *edit = qobject_cast(editor); edit->setText(index.data(Qt::EditRole).toString()); break; } case PropertyTree::SPINBOX: { QSpinBox *spin = qobject_cast(editor); spin->setValue(index.data(Qt::EditRole).toInt()); break; } case PropertyTree::DOUBLESPINBOX: { QDoubleSpinBox *doublespin = qobject_cast(editor); doublespin->setValue(index.data(Qt::EditRole).toDouble()); break; } case PropertyTree::FILE: { fileTypeSetEditorData(editor,index); break; } case PropertyTree::COLOR: { ColorDelegate *colordelegate = qobject_cast(editor); colordelegate->setColor(qvariant_cast(index.data())); break; } case PropertyTree::FONT: { QFontComboBox *fontbox = qobject_cast(editor); fontbox->setCurrentText(index.data(Qt::EditRole).toString()); break; } } } void TreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { int type = index.data(PropertyTree::ROLETYPE).toInt(); switch (type) { case PropertyTree::COMBOX: { QComboBox *box = qobject_cast(editor); model->setData(index,box->currentText()); break; } case PropertyTree::SPINBOX: { QSpinBox *spin = qobject_cast(editor); model->setData(index,spin->text()); break; } case PropertyTree::DOUBLESPINBOX: { QDoubleSpinBox *doublespin = qobject_cast(editor); model->setData(index,doublespin->text()); break; } case PropertyTree::TEXT: { QLineEdit *edit = qobject_cast(editor); model->setData(index,edit->text()); break; } case PropertyTree::FILE: { fileTypeSetModelData(editor,model,index); break; } case PropertyTree::COLOR: { ColorDelegate *colordelegate = qobject_cast(editor); model->setData(index,QVariant::fromValue(colordelegate->getColor())); break; } case PropertyTree::FONT: { QFontComboBox *fontbox = qobject_cast(editor); QString str = fontbox->currentText(); QTreeWidgetItem *item = static_cast(index.internalPointer()); if(item) { item->setFont(DELEGATE_COLUMN,fontbox->currentFont()); item->setText(DELEGATE_COLUMN,str); } break; } } } void TreeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index); editor->setGeometry(option.rect); } /*====================================================================================*/ //根据单元格参数类型创建代理的widget QWidget *TreeDelegate::creatrWidget(QWidget *parent, const QModelIndex &index) const { QWidget *widget = nullptr; int type = index.data(PropertyTree::ROLETYPE).toInt(); switch (type) { case PropertyTree::COMBOX: { QComboBox *box = new QComboBox(parent); QStringList list = index.data(PropertyTree::PARAMS).toStringList(); box->addItems(list); widget = box; break; } case PropertyTree::TEXT: { QLineEdit *edit = new QLineEdit(parent); widget = edit; break; } case PropertyTree::SPINBOX: { QSpinBox *spin = new QSpinBox(parent); widget = spin; break; } case PropertyTree::DOUBLESPINBOX: { QDoubleSpinBox *doublespin = new QDoubleSpinBox(parent); widget = doublespin; break; } case PropertyTree::FILE: { FileDelegate *filedelegate = new FileDelegate(parent); //filedelegate->setFocusPolicy(Qt::WheelFocus); connect(filedelegate,&FileDelegate::signal_finished,this,&TreeDelegate::slot_updateIndex); widget=filedelegate; break; } case PropertyTree::COLOR: { ColorDelegate *colordelegate = new ColorDelegate(parent); connect(colordelegate,&ColorDelegate::signal_colorChanged,this,&TreeDelegate::slot_updateIndex); widget = colordelegate; break; } case PropertyTree::FONT: { QFontComboBox *fontbox = new QFontComboBox(parent); widget = fontbox; break; } } return widget; }

懒得粘贴了,源码放这吧,需要的自行下载 源码下载



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3